package com.bl.student.task.config;

import com.bl.student.task.job.ArenaScheduleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * Created by yujingyi on 2018/3/15.
 */
@Configuration
//@ComponentScan(value = "com.bl.painters.task")
public class TaskConfig {

    static final private String JOB_TARGET_METHOD = "execute";

    static final private String ARENA_JOB_GROUP = "arena_job_group";
    static final private String ARENA_TRIGGER_GROUP = "arena_trigger_group";
    static final private String ARENA_SCHEDULE_JOB_NAME = "arena_schedule_job";
    static final private String ARENA_SCHEDULE_TRIGGER = "arena_schedule_trigger";

    @Autowired
    ArenaScheduleJob arenaScheduleJob;

    /**
     *  配置擂台调度job
     */
    @Bean(name = "arenaScheduleJobDetail")
    public MethodInvokingJobDetailFactoryBean arenaScheduleJobDetail() {
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        /*
         *  是否并发执行
         *  例如每5s执行一次任务，但是当前任务还没有执行完，就已经过了5s了，
         *  如果此处为true，则下一个任务会执行，如果此处为false，则下一个任务会等待上一个任务执行完后，再开始执行
         */
        jobDetail.setConcurrent(false);

        jobDetail.setGroup(ARENA_SCHEDULE_JOB_NAME);// 设置任务的分组，这些属性都可以存储在数据库中，在多任务的时候使用
        jobDetail.setName(ARENA_JOB_GROUP);// 设置任务的名字

        /*
         * 为需要执行的实体类对应的对象
         */
        jobDetail.setTargetObject(arenaScheduleJob);

        /*
         * sayHello为需要执行的方法
         * 通过这几个配置，告诉JobDetailFactoryBean我们需要执行定时执行ScheduleTask类中的sayHello方法
         */
        jobDetail.setTargetMethod(JOB_TARGET_METHOD);
        return jobDetail;
    }

    /**
     *  配置擂台调度job trigger
     */
    @Bean(name = "arenaScheduleJobTrigger")
    public CronTriggerFactoryBean arenaScheduleJobTrigger(MethodInvokingJobDetailFactoryBean arenaScheduleJobDetail) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setGroup(ARENA_TRIGGER_GROUP);
        trigger.setName(ARENA_SCHEDULE_TRIGGER);// trigger的name
        trigger.setJobDetail(arenaScheduleJobDetail.getObject());
        trigger.setCronExpression("0 */1 * * * ?    ");// 初始时的cron表达式 每隔10分钟执行一次
        return trigger;

    }

    /**
     *  配置job trigger
     */
    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactory(CronTriggerFactoryBean arenaScheduleJobTrigger) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
        bean.setOverwriteExistingJobs(true);
        // 延时启动，应用启动1秒后
        bean.setStartupDelay(1);
        // 注册触发器
        bean.setTriggers(arenaScheduleJobTrigger.getObject());
        return bean;
    }
}
